//
//  UserAccountManager.m
//  AppointmentForCar
//
//  Created by TheMacBook on 2016/11/8.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "UserAccountManager.h"
static UserAccountManager * manager;
#define kUserInfo @"userInfo"
@implementation UserAccountManager
+ (instancetype)shareUserManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[UserAccountManager alloc] init];
    });
    return manager;
}
- (instancetype)init
{
    self = [super init];
    if(self)
    {
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        NSDictionary * userDic = [userDefaults objectForKey:kUserInfo];
        if(userDic)
        {
            _user = [UserModel mj_objectWithKeyValues:userDic];
            _loginStatus = YES;
        }else
        {
            _loginStatus = NO;
        }

    }
    return self;
}
- (void)loginWithUserDic:(NSDictionary *)userDic
{
    NSLog(@"userDic = %@", userDic);
    [UserModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
        return @{
                 @"userId" : @"id",
                 @"address" : @"address",
                 @"avatarUrl" : @"avatarUrl",
                 @"gender" : @"gender",
                 @"nickName" : @"nickName",
                 @"phoneNumber" : @"phoneNumber"
                 };
    }];
    _user = [UserModel mj_objectWithKeyValues:userDic];
    NSLog(@"user = %@", _user);
    _loginStatus = YES;
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:userDic forKey:kUserInfo];
}
- (void)logout
{
    _user = nil;
    _loginStatus = NO;
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:kUserInfo];
}
@end
